package com.itmiaojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmiaojie.handle.exception.SystemException;
import com.itmiaojie.pojo.AppHttpCodeEnum;
import com.itmiaojie.pojo.Comment;
import com.itmiaojie.pojo.ResponseResult;
import com.itmiaojie.pojo.User;
import com.itmiaojie.pojo.vo.CommentVo;
import com.itmiaojie.pojo.vo.PageVo;
import com.itmiaojie.service.CommentService;
import com.itmiaojie.mapper.CommentMapper;
import com.itmiaojie.service.UserService;
import com.itmiaojie.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
    @Autowired
    private UserService userService;
    @Override
    public ResponseResult commentList(String type, Long articleId, Integer pageNum, Integer pageSize) throws IllegalAccessException, InstantiationException {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq("0".equals(type),Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getType,type);
        //先查根评论
        queryWrapper.eq(Comment::getRootId,-1);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        baseMapper.selectPage(page, queryWrapper);
        List<Comment> rootComments = page.getRecords();
        List<CommentVo> commentVoList = toCommentVoList(rootComments);
        for (CommentVo commentVo : commentVoList) {
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult(200,"评论成功");
    }

    private List<CommentVo> getChildren(Long id) {


        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        List<CommentVo> commentVoList = null;
        try {
            commentVoList = toCommentVoList(comments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commentVoList;

    }

    private List<CommentVo> toCommentVoList(List<Comment> rootComments) throws InstantiationException, IllegalAccessException {

        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(rootComments, CommentVo.class);
        for (CommentVo commentVo : commentVoList) {
            log.info(commentVo.getCreateBy().toString());
            User user = userService.getById(commentVo.getCreateBy());
            log.info(user.toString());
            commentVo.setUsername(user.getUserName());
            if (commentVo.getToCommentUserId() != -1){
                commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getUserName());
            }
        }
        return commentVoList;
    }
}




